<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends CommonController {
    public function index(){
        $this->display();
    }
    public function hxc_dev(){
        $this->display();
    }
    public function home_page(){
        $get = I('data');
        $get = $this->jiemi($get);
        $vid = $get['user_id'];
        $system =$get['system'];
        if($system=='2'){
            $list = M('home_page','',C('DB2'))->where(array('status'=>'1','type'=>'1'))->field('title,url,pic,content,ishead,islogin')->select();
        }else{
            $list = M('home_page','',C('DB2'))->where(array('status'=>'1','type'=>'2'))->field('title,url,pic,content,ishead,islogin')->select();
        }
        $userdata = M('user','',C('DB2'))->where(array('id'=>$vid))->field('id as vid,username as name,identity_num,score,evaluat_status,mobile,user_msg_verify,mobile_verify,contact_list,urgent_verify,company_verify')->find();
        if($userdata['user_msg_verify']=='1'){
            foreach ($list as $k=>$v){
//                if($vid=='273'){
                    $list[$k]['url']='https://hxgj.dstlkj.com/Dev/hxc_dev#/idcard';
//                }else{
//                    $list[$k]['url']='https://hxgj.dstlkj.com/index/index#/idcard';
//                }
            }
        }elseif ($userdata['mobile_verify']=='1'){
            foreach ($list as $k=>$v){
                if($v['title']=='通讯录评测'||$v['title']=='社交评测'){
                    $list[$k]['url']= 'https://open.shujumohe.com/box/yys?box_token=16A9D98E0C2049CBBCAD60396070BD05&cb=https://hxgj.dstlkj.com/Usercheck/call_back&real_name='.$userdata['name'].'&user_mobile='.$userdata['mobile'].'&identity_code='.$userdata['identity_num'].'&passback_params='.$vid;
                }
            }
        }
        $isnull = empty($userdata)?'1':'0';
        switch ($userdata['evaluat_status']){
            case '1':
                $userdata['index_content']='等待评测';
                $userdata['button']='开始评测';
                if($vid=='273'){
                    $userdata['link'] ='https://hxgj.dstlkj.com/Dev/hxc_dev#/idcard';
                }else{
                    $userdata['link'] = 'https://hxgj.dstlkj.com/index/index#/idcard';
                }
//                $userdata['link'] = 'https://hxgj.dstlkj.com/index/index#/idcard';
                $userdata['ishead']='1';
                $userdata['islogin']='1';
                break;
            case '2':
                if($userdata['urgent_verify']=='0'&&$vid>=273){
                    $userdata['link'] = 'https://hxgj.dstlkj.com/Dev/hxc_dev#/call';
                    $userdata['title'] = '紧急联系人';
                }elseif($userdata['company_verify']=='0'){
                    $userdata['link'] = 'https://hxgj.dstlkj.com/Dev/hxc_dev#/company';
                    $userdata['title'] = '公司信息';
                }elseif($userdata['contact_list']=='1'){
                    $userdata['link'] = 'telbookUpload';
                }else{
                    if($userdata['mobile_verify']=='1'){
                        $userdata['link'] = 'https://open.shujumohe.com/box/yys?box_token=16A9D98E0C2049CBBCAD60396070BD05&cb=https://hxgj.dstlkj.com/Usercheck/call_back&real_name='.$userdata['name'].'&user_mobile='.$userdata['mobile'].'&identity_code='.$userdata['identity_num'].'&passback_params='.$vid;
//
                        $userdata['title'] = '运营商评测';
                    }elseif($userdata['mobile']=='2'){
                        $userdata['link'] = '#';
                        $userdata['title'] = '运营商评测';
                        $userdata['code']='测评中请等待结果';
                    }else{
                        $userdata['link'] = '#';
                        $userdata['title'] = '运营商评测';
                        $userdata['code']='测评中请等待结果';
                    }
                }
                if($userdata['mobile_verify']=='2'||$userdata['mobile_verify']=='3'){
                    $userdata['index_content']='正在计算分值';
                    $userdata['button']='评测中';
                    $userdata['ishead']='1';
                    $userdata['islogin']='1';
                }else{
                    $userdata['index_content']='等待评测';
                    $userdata['button']='继续评测';
                    $userdata['ishead']='1';
                    $userdata['islogin']='1';
                }
                break;
            default:
                $userdata['index_content']='信用评分很好哦';
                $userdata['button']='#';
                $userdata['link'] = '';
                $userdata['ishead']='1';
                $userdata['islogin']='1';
                break;
        }

        if(empty($vid)){
            $data['ret']='1001';
            $data['code']='成功';
            $data['isnull']=$isnull;
            $userdata['index_content']='等待评测';
            $userdata['button']='开始评测';
            $userdata['link'] = '#';
            $userdata['ishead']='1';
            $userdata['islogin']='1';
            $data['data']['page_data']=$list;
            $data['data']['user_data']=$userdata;
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }else{
            $data['ret']='1001';
            $data['code']='成功';
            $data['isnull']=$isnull;
            $data['data']['page_data']=$list;
            $data['data']['user_data']=$userdata;
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
    }

    public function edit_status(){
        M('user','',C('DB2'))->where(array('evaluat_status'=>'2','user_msg_verify'=>'2','mobile_verify'=>'3','contact_list'=>'2'))->save(array('score'=>rand(500,690),'evaluat_status'=>'3'));
    }
    public function edit_status1(){
        $data = M('user','',C('DB2'))->where(array('evaluat_status'=>'2','user_msg_verify'=>'2','mobile_verify'=>'3','contact_list'=>'2'))->field('id,mobile')->selct();

        foreach ($data as $k=>$v){
            $post['mobile']=$v['mobile'];
            $post['mobilebook']=M('mobilebook','',C('DB2_ZX'))->where(array('user_id'=>$v['id']))->field('mail_name as name,mail_mobile as mobile')->select();
            $post['mobilecall'] =M('mobile_callrecord','',C('DB2_ZX'))->where(array('mymobile'=>$v['mobile']))->field('call_other_number,call_type_name,call_start_time,call_time')->select();
            $post['get_time'] = M('mobile_basicinfo','',C('DB2_ZX'))->where(array('user_id'=>$v['id']))->getField('ctime');
            $url = 'https://jallzf.miaodaoxj.com:444/xycsf/getCheatCard';
            $res = request_post($url,$post);
            file_put_contents('./fqz.txt','user_id:'.$v['mobile'].'结果:'.$res,FILE_APPEND);
            $res = json_decode($res,true);
            M('user','',C('DB2'))->where(array('id'=>$v['id']))->save(array('score'=>$res['data']['score']));
        }
    }
    public function fzpc(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '367';
        $vid = $get['vid'];
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id))->field('username,mobile,identity_num')->find();
        if(empty($vid)||$vid=='0a4e7ad315dacfddbe8cae58603f341f'){
            $data['ret']='1001';
            $data['code']='成功';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        if(empty($user_data['username'])||empty($user_data['identity_num'])){
            $data['ret']='1002';
            $data['code']='请先完善基础信息';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        $user_jjd_jdb = M('scrapy_jjd','',C('DB2_ZX'))->field('n_reported_cnt,n_current_borrow_amt,n_current_overdue_amt,n_current_overdue_cnt,n_borrow_amt,n_borrow_cnt,n_borrow_from_person_cnt,n_history_overdue_max')->find();
        if(empty($user_jjd_jdb)){
            $url_jjd='http://116.62.146.43:8399/xyc/jjd?mobile='.$user_data['mobile'];
            $res1 = $this->getcurl($url_jjd);
            $res1 = json_decode($res1,true);
            $add1 = array(
                'mobile'=>$user_data['mobile'],
                'n_reported_cnt'=>$res1['n_reported_cnt'],
                'n_current_borrow_amt'=>$res1['n_current_borrow_amt'],
                'n_current_overdue_amt'=>$res1['n_current_overdue_amt'],
                'n_current_overdue_cnt'=>$res1['n_current_overdue_cnt'],
                'n_borrow_amt'=>$res1['n_borrow_amt'],
                'n_borrow_cnt'=>$res1['n_borrow_cnt'],
                'n_borrow_from_person_cnt'=>$res1['n_borrow_from_person_cnt'],
                'n_history_overdue_max'=>$res1['n_history_overdue_max'],
                'ctime'=>time(),
            );
            M('scrapy_jjd','',C('DB2_ZX'))->add($add1);
            $user_jjd_jdb = M('scrapy_jjd','',C('DB2_ZX'))->field('n_reported_cnt,n_current_borrow_amt,n_current_overdue_amt,n_current_overdue_cnt,n_borrow_amt,n_borrow_cnt,n_borrow_from_person_cnt,n_history_overdue_max')->find();
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['data']=$user_jjd_jdb;
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }else{
            $data['ret']='1001';
            $data['code']='success';
            $data['data']=$user_jjd_jdb;
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
    }
    public function bad_recode1(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '367';
        $vid =$get['vid'];
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id))->field('username,mobile,identity_num')->find();
        if(empty($vid)||$vid=='0a4e7ad315dacfddbe8cae58603f341f'){
            $data['ret']='1001';
            $data['code']='成功';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        if(empty($user_data['username'])||empty($user_data['identity_num'])){
            $data['ret']='1002';
            $data['code']='请先完善基础信息';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        $user_jjd_jdb = M('scrapy_jdb','',C('DB2_ZX'))->where(array('mobile'=>$user_data['mobile']))->field('message,name,sfz')->find();
        if(empty($user_jjd_jdb['message'])){
            $url_jdb='http://116.62.146.43:8399/xyc/jdb?mobile='.$user_data['mobile'];
            $res = $this->getcurl($url_jdb);
            $res = json_decode($res,true);
            $add = array(
                'mobile'=>$user_data['mobile'],
                'message'=>$res['message'],
                'sfz'=>$res['data']['sfz'],
                'name'=>$res['data']['name'],
                'status'=>$res['status'],
                'ctime'=>time(),
            );
            M('scrapy_jdb','',C('DB2_ZX'))->add($add);
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['message']=$res['message'];
            $data['data']['count']=$res['message']=='查无结果'?'0':'1';
            $data['data']['sfz']=empty($res['data']['sfz'])?'无数据':$res['data']['sfz'];
            $data['data']['name']=empty($res['data']['name'])?'无数据':$res['data']['name'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }else{
            $data['ret']='1001';
            $data['code']='success';
            $data['data']=$user_jjd_jdb;
            $data['data']['count']=$user_jjd_jdb['message']=='查无结果'?'0':'1';
            $data['data']['sfz']=empty($user_jjd_jdb['sfz'])?'无数据':$user_jjd_jdb['sfz'];
            $data['data']['name']=empty($user_jjd_jdb['name'])?'无数据':$user_jjd_jdb['name'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
    }
    public function bad_recode(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
        $user_id = '385';
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id))->field('username,mobile,identity_num')->find();
        if(empty($user_data['username'])||empty($user_data['identity_num'])){
            $data['ret']='1002';
            $data['code']='请先完善基础信息';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        $user_jjd_jdb = M('user_credit','',C('DB2'))->where(array('name'=>$user_data['username'],'idcard'=>$user_data['identity_num']))->field('courtName,disruptTypeName,id')->find();
        if(empty($user_jjd_jdb['id'])){
            $user_data['username']='张家禹';
            $user_data['username']=urlencode($user_data['username']);
            $user_data['identity_num'] = '352227199402131315';
            $url_jdb='http://116.62.146.43:8399/xyc/court?name='.$user_data['username'].'&idcard='.$user_data['identity_num'];
//            $url_jdb='http://116.62.146.43:8399/xyc/court?name=张家禹&idcard=352227199402131315';
            $res = $this->getcurl($url_jdb,'',array('Content-type: application/json','charset:utf-8'));
            $res = json_decode($res,true);

            $add = array(
                'name'=>$user_data['username'],
                'idcard'=>$user_data['identity_num'],
                'StdStg'=>$res['data']['StdStg'],
                'name'=>$res['data']['name'],
                'status'=>$res['status'],
                'ctime'=>time(),
            );
            M('scrapy_jdb','',C('DB2_ZX'))->add($add);
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['message']=$res['message'];
            $data['data']['count']=$res['message']=='查无结果'?'0':'1';
            $data['data']['sfz']=empty($res['data']['sfz'])?'无数据':$res['data']['sfz'];
            $data['data']['name']=empty($res['data']['name'])?'无数据':$res['data']['name'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }else{
            $data['ret']='1001';
            $data['code']='success';
            $data['data']=$user_jjd_jdb;
            $data['data']['count']=$user_jjd_jdb['message']=='查无结果'?'0':'1';
            $data['data']['sfz']=empty($user_jjd_jdb['sfz'])?'无数据':$user_jjd_jdb['sfz'];
            $data['data']['name']=empty($user_jjd_jdb['name'])?'无数据':$user_jjd_jdb['name'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
    }
    public function getcurl($url = '',$data='',$header='')
    {
        $ch = curl_init();
        if (!empty($header)) {
//            $header=array('Content-type: application/json','charset:utf-8'); 示例
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, '1');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $res = curl_exec($ch);
        if (curl_error($ch)) {
            return '错误信息:' . curl_error($ch);
        }
        curl_close($ch);
        return $res;

    }
    function post2($url, $data){//file_get_content

        $postdata = http_build_query(
            $data
        );
        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );
        $context = stream_context_create($opts);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    public function get_pfk(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '367';
        if(empty($user_id)){
            $data['ret']='1002';
            $data['code']='请先登陆';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id))->field('apply_grade,apply_score,username,identity_num')->find();
        if(empty($user_data['apply_grade'])||empty($user_data['apply_score'])){
            $task_id =M('tdyyshd_report','',C('DB2_ZX'))->where(array('user_id'=>$user_id))->getField('task_id');
            $post_data['task_id'] =$task_id;
            $url = 'https://api.shujumohe.com/octopus/report.task.query/v2?partner_code=sharedcash&partner_key=dfc99c9bad014db59fc7ed6d4187ae49';
            $result = $this->post2($url,$post_data);
            $result = json_decode($result,true);
            $str = base64_decode($result['data']);
            $str = gzdecode($str);
            $str = json_decode($str,true);
            $add_app['call_count_late_night_3month'] = empty($str['all_contact_stats']['call_count_late_night_3month'])?'0':$str['all_contact_stats']['call_count_late_night_3month'];
            $add_app['contact_count_mutual_3month'] = empty($str['all_contact_stats']['contact_count_mutual_3month'])?'0':$str['all_contact_stats']['contact_count_mutual_3month'];
            $add_app['call_count_call_time_less1min_6month'] = empty($str['all_contact_stats']['call_count_call_time_less1min_6month'])?'0':$str['all_contact_stats']['call_count_call_time_less1min_6month'];
            $add_app['call_count_6month'] = empty($str['all_contact_stats']['call_count_6month'])?'0':$str['all_contact_stats']['call_count_6month'];
            $add_app['contact_count_call_count_over10_6month'] = empty($str['all_contact_stats']['contact_count_call_count_over10_6month'])?'0':$str['all_contact_stats']['contact_count_call_count_over10_6month'];
            $add_app['call_count_call_time_over10min_6month'] = empty($str['all_contact_stats']['call_count_call_time_over10min_6month'])?'0':$str['all_contact_stats']['call_count_call_time_over10min_6month'];
            $add_app['call_count_active'] = empty($str['all_contact_stats_per_month']['call_count_active'])?'0':$str['all_contact_stats_per_month']['call_count_active'];
            $add_app['contact_count_active'] = empty($str['all_contact_stats_per_month']['contact_count_active'])?'0':$str['all_contact_stats_per_month']['contact_count_active'];
            $add_app['call_count_late_night'] = empty($str['all_contact_stats_per_month']['call_count_late_night'])?'0':$str['all_contact_stats_per_month']['call_count_late_night'];
            $add_app['call_count'] = empty($str['all_contact_stats_per_month']['call_count'])?'0':$str['all_contact_stats_per_month']['call_count'];
            $add_app['sex'] = $str['user_info']['gender'];
            $add_app['age'] = $str['user_info']['age'];
            $add_app['ctime'] = M('user', '', C('DB2'))->where(array('id' => $user_id))->getField('ctime');
            $add_app['td_7days'] = M('get_td_data', '', C('DB2_ZX'))->where(array('user_id' => $user_id))->getField('seven_day');
            $add_app['mobile_net_age'] = empty($str['mobile_info']['mobile_net_age'])?'0':$str['mobile_info']['mobile_net_age'];
            $add_app['consume_amount_3month'] = empty($str['carrier_consumption_stats']['consume_amount_3month'])?'0':$str['carrier_consumption_stats']['consume_amount_3month'];
            $a = json_encode($add_app,JSON_UNESCAPED_UNICODE);
//            var_dump($a);die;
            M('user_applycard', '', C('DB2'))->add($add_app);
            $url = 'https://jallzf.miaodaoxj.com:444/jyyjr/xycsf/getApplyCard';
            $res = $this->getcurl($url,$a,array('Content-type: application/json','charset:utf-8'));
            $res = json_decode($res,true);
//            var_dump($res);die;
            $ress = M('user','',C('DB2'))->where(array('id'=>$user_id))->save(array('apply_score'=>$res['data']['score'],'apply_grade'=>$res['data']['grade']));
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['apply_score']=$res['data']['score'];
            $data['data']['apply_grade']=$res['data']['grade'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }else{
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['apply_score']=$user_data['apply_score'];
            $data['data']['apply_grade']=$user_data['apply_grade'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
    }

    public function get_cheatcard(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
        $user_id = '367';
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id))->field('id,username,mobile,identity_num')->find();
        if(empty($user_data['username'])||empty($user_data['identity_num'])){
            $data['ret']='1002';
            $data['code']='请先完善基础信息';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
        $post['mobile']=$user_data['mobile'];
        $post['mobilebook']=M('mobilebook','',C('DB2_ZX'))->where(array('user_id'=>$user_data['id']))->field('mail_name as name,mail_mobile as mobile')->select();
        $post['mobilecall'] =M('mobile_callrecord','',C('DB2_ZX'))->where(array('mymobile'=>$user_data['mobile']))->field('call_other_number,call_type_name,call_start_time,call_time')->limit(4)->select();
        $post['get_time'] = M('mobile_basicinfo','',C('DB2_ZX'))->where(array('user_id'=>$user_data['id']))->getField('ctime');
        $idcardaddress = $this->getidecardaddress($user_data['identity_num']);
        $post['province']=$idcardaddress['shenfen'];
        $date = strtotime(substr($user_data['identity_num'], 6, 8));
        //获得出生年月日的时间戳
        $today = strtotime('today');
        //获得今日的时间戳
        $diff = floor(($today - $date) / 86400 / 365);
        //得到两个日期相差的大体年数
        //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
        $age = strtotime(substr($user_data['identity_num'], 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
        $post['age']=$age;
        $post['ismarry']='';
        $a = json_encode($post,JSON_UNESCAPED_UNICODE);
//        var_dump($a);die;
        $url = 'https://jallzf.miaodaoxj.com:444/jyyjr/xycsf/getCheatCard';
        $res = $this->getcurl($url,$a,array('Content-type: application/json','charset:utf-8'));
//        $res = json_decode($res,true);
        var_dump($res);
    }
    function getidecardaddress($idcard){
        $data = idcardaddress($idcard);
        if(mb_strpos($data['city'],'内蒙古自治区')!==false){
            $position='内蒙古自治区';
        }elseif(mb_strpos($data['city'],'新疆维吾尔自治区')!==false){
            $position='新疆维吾尔自治区';
        }elseif(mb_strpos($data['city'],'广西壮族自治区')!==false){
            $position='广西壮族自治区';
        }elseif(mb_strpos($data['city'],'宁夏回族自治区')!==false){
            $position='宁夏回族自治区';
        }elseif(mb_strpos($data['city'],'西藏自治区')!==false){
            $position='西藏自治区';
        }else{
            $city = mb_strpos($data['city'],'省');
            $data['city'] = substr($data['city'],$city+7);
            $city    =mb_strpos($data['city'],'市');
            if($city===false){
                $xian=mb_strpos($data['city'],'县');
                if($xian===false){
                    $qu=mb_strpos($data['city'],'区');
                    if($qu===false){
                        $cun=mb_strpos($data['city'],'村');
                        if($cun===false){
                            $province=mb_strpos($data['city'],'省');
                            $data['city']=mb_substr($data['city'],0,($province+1),'utf-8');
                        }else{
                            $data['city']=mb_substr($data['city'],0,($cun+1),'utf-8');
                        }
                    }else{
                        $data['city']=mb_substr($data['city'],0,($qu+1),'utf-8');
                    }
                }else{
                    $data['city']=mb_substr($data['city'],0,($xian+1),'utf-8');
                }
            }else{
                $data['city']=mb_substr($data['city'],0,($city+1),'utf-8');
            }
        }
        return $data;
    }
}